package com.sk.commonadapter_android.adapter.refresh;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.commonadapter_android.R;

/**
 * @Author yemao
 * @Email yrmao9893@163.com
 * @Date 2017/7/5
 * @Des null!
 */

public class RefreshHeaderLayout extends LinearLayout implements BaseRefreshImp {
    private Context mContext;
    private AnimationDrawable animationDrawable;
    private TextView mTextView;
    private ImageView mImg;
    private int mState = STATE_NORMAL;
    private int mMeasuredHeight;
    private LinearLayout mContainer;
    private RotateAnimation mRotateAnimation;

    public RefreshHeaderLayout(Context context) {
        this(context, null);
    }

    public RefreshHeaderLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshHeaderLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.common_refresh_header, this);
        mTextView = (TextView) findViewById(R.id.tv_refresh_msg);
        mImg = (ImageView) findViewById(R.id.iv_refresh_img);
        mContainer = (LinearLayout) findViewById(R.id.container);
        measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mMeasuredHeight = getMeasuredHeight();
        setState(STATE_NORMAL);
        mContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0));
        this.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        //初始化动画

        mRotateAnimation = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        mRotateAnimation.setInterpolator(lin);
        mRotateAnimation.setDuration(1500);//动画转动一次的时间
        mRotateAnimation.setRepeatCount(-1);//设置重复次数
        mRotateAnimation.setFillAfter(true);//动画执行完后是否停留在执行完的状态
//        mRotateAnimation.setStartOffset(10);//设置起始偏移量

    }

    public void setState(int state) {
        switch (state) {
            case STATE_NORMAL:
                mTextView.setText(mContext.getString(R.string.listview_header_hint_normal));
                break;
            case STATE_RELEASE_TO_REFRESH:
                mTextView.setText(mContext.getString(R.string.listview_header_hint_release));
                break;
            case STATE_REFRESHING:
                mTextView.setText(mContext.getString(R.string.refreshing));
                mImg.startAnimation(mRotateAnimation);
                break;
            case STATE_DONE:
                mImg.clearAnimation();
                mTextView.setText(mContext.getString(R.string.refresh_done));
                break;
        }
        mState = state;
    }

    @Override
    public void onMove(float delta) {
        if (getVisiableHeight() > 0 || delta > 0) {
            setVisiableHeight(((int) delta) + getVisiableHeight());
            if (mState <= STATE_RELEASE_TO_REFRESH) {
                //处于未刷新状态
                if (getVisiableHeight() > mMeasuredHeight) {
                    setState(STATE_RELEASE_TO_REFRESH);
                } else {
                    setState(STATE_NORMAL);
                }

            }

        }

    }

    @Override
    public boolean releaseAction() {
        boolean isOnRefresh = false;
        int height = getVisiableHeight();
        if (height == 0) // not visible.
            isOnRefresh = false;

        if (getVisiableHeight() > mMeasuredHeight && mState < STATE_REFRESHING) {
            setState(STATE_REFRESHING);
            isOnRefresh = true;
        }
        // refreshing and header isn't shown fully. do nothing.
        if (mState == STATE_REFRESHING && height <= mMeasuredHeight) {
            //return;
        }
        int destHeight = 0; // default: scroll back to dismiss header.
        // is refreshing, just scroll back to show all the header.
        if (mState == STATE_REFRESHING) {
            destHeight = mMeasuredHeight;
        }
        smoothScrollTo(destHeight);

        return isOnRefresh;
    }

    private void smoothScrollTo(int destHeight) {
        ValueAnimator animator = ValueAnimator.ofInt(getVisiableHeight(), destHeight);
        animator.setDuration(300).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setVisiableHeight((int) animation.getAnimatedValue());
            }
        });
        animator.start();
    }

    @Override
    public void refreshComplate() {
        setState(STATE_DONE);
        new Handler().postDelayed(new Runnable() {
            public void run() {
                reset();
            }
        }, 500);
    }

    public void reset() {
        smoothScrollTo(0);
        setState(STATE_NORMAL);
    }

    @Override
    public int getVisiableHeight() {
        return mContainer.getHeight();
    }

    public void setVisiableHeight(int height) {
        if (height < 0)
            height = 0;
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0));
        lp.height = height;
        mContainer.setLayoutParams(lp);
    }

    public int getState() {
        return mState;
    }
}
